package com.gugee.tiktok.web.controller.web;

import com.gugee.common.web.util.CheckMobile;
import com.gugee.common.web.util.jwt.JwtUtil;
import com.gugee.common.web.system.product.dto.ProductResponse;
import com.gugee.common.web.system.product.service.ProductService;
import com.gugee.common.web.system.user.dto.UserReturnDto;
import com.gugee.common.web.system.user.model.User;
import com.gugee.common.web.system.user.service.UserService;
import com.xiguaji.boot.toolkit.HttpUtil;
import com.xiguaji.boot.toolkit.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@Slf4j
public class ChargeController {

    @Value("${jwt.head}")
    String authorized;

    @Value("${server.domain}")
    String serverDomain;

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    ProductService productService;

    @Autowired
    UserService userService;

    /**
     * 产品页面
     * @return
     */
    @GetMapping("/pricing")
    public ModelAndView pricing(){
        ModelAndView modelAndView = new ModelAndView("pricing/index");
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("tab",10);
        modelAndView.addObject("contextPath",serverDomain);
        UserReturnDto user = getUser();
        modelAndView.addObject("login", user != null);
        modelAndView.addObject("user", user);
        modelAndView.addObject("reportProduct",productService.getReportProductList());
        List<ProductResponse> productResponses;

        if(null == user){
            productResponses = productService.getVersionProductList(0);
        }else {
            productResponses = productService.getVersionProductList(getUser().getId());
        }

        modelAndView.addObject("versionProduct",productResponses);
        return modelAndView;
    }

    private UserReturnDto getUser() {
        String token = HttpUtil.getCookie(authorized);

        if (StringUtils.isEmpty(token)) {
            return null;
        }
        User user = JsonUtil.jsonParse(User.class, JsonUtil.parseJson(jwtUtil.getJwtUser(token).getUser()));
        return userService.getUser(user.getId());
    }
}
